package TUILayer;
import java.util.Scanner;
import ControlLayer.VareCtr;
import java.util.ArrayList;
import ModelLayer.*;
/**
 * User interface to the Vare controller
 */
public class VareUI
{
    private VareCtr vareCtr;//Vare UI must know the Vare controller
    private Vare varer;
   
    public VareUI()
    {
        vareCtr = new VareCtr();
    }
    
    public void start()
    {
        vareMenu();
        vareCtr = new VareCtr();
    }
    
    public void vareMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choice = writeVareMenu();
            if (choice == 1)
            {
                findVare();
            }
            else{   
                if(choice == 2)
                {
                    addVare();
                }
                else{   
                    if(choice == 3)
                    {
                        listeVare();
                    }
                    else{
                        if(choice == 4)
                        {
                            updateVare();
                        }
                        else
                            {
                                exit = true;
                            }
                        }
                    }
                }
            }
        }
        
    
    public int writeVareMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" *** Vare Menu *** ");
        System.out.println(" (1) Find vare");
        System.out.println(" (2) Add vare");
        System.out.println(" (3) Vare liste");
        System.out.println(" (4) Opdater vare");
        System.out.println(" (5) Tilbage");
        System.out.println("\n Vælg: ");
        System.out.println();
        
        int choice = keyboard.nextInt();
        return choice;
    }
    
   public int inputVareNr()
    {   
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Indtast Vare nr:  ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Der skal indtastes et tal -  prøv igen");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
    
    public int inputIndexNo()
    {   
        boolean ok = false;
        int index = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Indtast nr.:  ");
           try{
             index = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Der skal indtastes et tal -  prøv igen");
             String input = keyboard.nextLine();
           }
        }//end while
        return index;
    }
    
    
    public int inputVareNummer()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Vare nummer:  ");
        int nummer = keyboard.nextInt();
        return nummer;
    }
    public String inputVareNavn()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Vare navn:  ");
        String name = keyboard.nextLine();
        return name;
    }
    
     public int inputVareAntal()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Vare antal:  ");
        int antal = keyboard.nextInt();
        return antal;
    }
    
     public void showVareNr(int nr)
    {
        System.out.println("vares NR: " + nr);
    }
    public void showVareNavn(String navn)
    { 
        System.out.println("Vares navn: " + navn);
    }
    
    private void findVare()
    {
        String navn = inputVareNavn();
        ArrayList<Vare> vare = vareCtr.listeAfVarer();
        Vare varer = vareCtr.findVare(navn);
        System.out.println("\n" + varer.getVareNummer() + " " + varer.getVareNavn()) ;
        
        if(varer == null)
        {
            System.out.println("Kan ikke finde vare");
        }
    }
    
    private void addVare()
    {
        int vareNummer = inputVareNummer();
        String vareNavn =  inputVareNavn();
        int lagerAntal = inputVareAntal();
        
        vareCtr.addVare(vareNummer, vareNavn, lagerAntal);
    }
    
    
    
    private void listeVare()
    {
         ArrayList<Vare> vare = vareCtr.listeAfVarer();
         int index = 0;
         for(Vare varer : vare)
         {
               System.out.println("\n" + index + " " + varer.getVareNummer() + " " + varer.getVareNavn() + " " + varer.getLagerAntal());
               index ++;
         }
         
         System.out.println();
    }
    
    private void updateVare()
    {
        int nr = inputVareNr();
        String VareNavn = inputVareNavn();
        if(VareNavn.length() > 0)
        {
            varer.setVareNavn(VareNavn);
        }
        
    }
    
 }

